//
//  LXMainViewController.m
//  cat
//
//  Created by imera on 16/1/8.
//  Copyright © 2016年 LX. All rights reserved.
//
#import "LXMainViewController.h"
#import "LXOperaTableViewController.h"
#import "LXRecommendTableViewController.h"
#import "LXMineTableViewController.h"
#import "LXTypeTableViewController.h"
#import "LXEntertainmentTableViewController.h"
#import "LXMovieShowViewController.h"
#import "LXVideoShowViewController.h"
#import "LXMainNavBarView.h"
#import "UIImage+Lx.h"
#import "LXSearchTableViewController.h"
#import "LXVideo.h"
#import "LXTypeShowViewController.h"
#import "LXLoginViewController.h"

@interface LXMainViewController ()<UIScrollViewDelegate,LXMainNavBarViewDelegate,LXMineTableViewControllerDelegate>
/** 切换卡数据 */
@property (nonatomic, strong) NSArray *tabArray;
/** 栏目头标题 */
@property (nonatomic, strong) NSArray *titleArray;
/** 当前点击的按钮 */
@property (nonatomic, strong) UIButton *currentBtn;
/** 按钮数组 */
@property (strong, nonatomic) NSMutableArray *topBtns;

///@brife 下方的ScrollView
@property (strong, nonatomic) UIScrollView *scrollView;

///@brife 下方的表格数组
@property (strong, nonatomic) NSMutableArray *scrollTableViewsController;

///@brife TableViews的数据源
@property (strong, nonatomic) NSMutableArray *dataSource;

///@brife 当前选中页数
@property (assign) NSInteger currentPage;

///@brife 下面滑动的View
@property (strong, nonatomic) UIView *slideView;

///@brife 上方的ScrollView
@property (strong, nonatomic) UIScrollView *topScrollView;

///@brife 上方的view
@property (strong, nonatomic) UIView *topMainView;

@end

@implementation LXMainViewController

- (NSArray *)tabArray
{
    if(_tabArray == nil){
        _tabArray = [NSArray arrayWithObjects:@"推荐", @"动画", @"音乐", @"分区", @"我的",nil];
    }
    return _tabArray;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.topBtns = [[NSMutableArray alloc] init];
    self.scrollTableViewsController = [[NSMutableArray alloc] init];
    
    [self initScrollView];
    
    [self initTopTabs];
    
    [self initDownTables];
    
    [self initSlideView];
    
    /** 视频展示 */
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlay:) name:@"moviePlay" object:nil];
    /** 分类cell被点击 */
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(cellClick:) name:@"typeCellClick" object:nil];
}

#pragma mark - 视频展示
- (void)moviePlay:(NSNotification *)notification
{
    LXVideo *video = (LXVideo *)notification.userInfo[@"video"];
    LXVideoShowViewController *videoShowController = [[LXVideoShowViewController alloc]init];
    videoShowController.video = video;
    [self.navigationController pushViewController:videoShowController animated:YES];
}

#pragma mark - 分类视频点击
- (void)cellClick:(NSNotification *)notification
{
    int typeID = (int)[notification.userInfo[@"typeID"] integerValue];
    LXTypeShowViewController *typeShowVc = [[LXTypeShowViewController alloc]initWithTypeID:typeID];
    [self.navigationController pushViewController:typeShowVc animated:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark -- 初始化滑动的指示View
- (void)initSlideView
{
    CGFloat width = LXMainViewControllerViewWidth / 6;
    
    if(self.tabArray.count <=6){
        width = LXMainViewControllerViewWidth / self.tabArray.count;
    }
    
    self.slideView = [[UIView alloc] initWithFrame:CGRectMake(0, TOPHEIGHT - 3, width, 3)];
    [self.slideView setBackgroundColor:MainBackgroundBlueColor];
    [self.topScrollView addSubview:self.slideView];
}

#pragma mark -- 实例化顶部的tab
- (void)initTopTabs
{
    CGFloat width = LXMainViewControllerViewWidth / 6;
    
    if(self.tabArray.count <=6)
    {
        width = LXMainViewControllerViewWidth / self.tabArray.count;
    }
    self.topMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, LXMainViewControllerViewWidth, LXMainViewControllerTopMainViewHeight)];
    [self.view addSubview:self.topMainView];
    
    /*** 顶部导航栏 */
    CGFloat navBarViewX = 0;
    CGFloat navBarViewY = 0;
    CGFloat navBarViewW = LXMainViewControllerViewWidth;
    CGFloat navBarViewH = 70;
    LXMainNavBarView *navBarView = [[LXMainNavBarView alloc]initWithFrame:CGRectMake(navBarViewX, navBarViewY, navBarViewW, navBarViewH)];
    navBarView.MyDelagate = self;
    navBarView.backgroundColor = MainBackgroundBlueColor;
    [self.topMainView addSubview:navBarView];
    
    self.topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, navBarViewH + 1, LXMainViewControllerViewWidth, TOPHEIGHT)];
    self.topScrollView.showsHorizontalScrollIndicator = NO;
    self.topScrollView.showsVerticalScrollIndicator = YES;
    self.topScrollView.bounces = NO;
    self.topScrollView.delegate = self;

    if (self.tabArray.count >= 6)
    {
        self.topScrollView.contentSize = CGSizeMake(width * self.tabArray.count, TOPHEIGHT);
        
    } else {
        self.topScrollView.contentSize = CGSizeMake(LXMainViewControllerViewWidth, TOPHEIGHT);
    }
    
    [self.topMainView addSubview:self.topScrollView];
    
    for (int i = 0; i < self.tabArray.count; i ++)
    {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, width, TOPHEIGHT)];

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, TOPHEIGHT)];
        button.tag = i;
        button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage resizeWithName:@"t"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage resizeWithName:@"tab"] forState:UIControlStateSelected];
        [button setTitle:[NSString stringWithFormat:@"%@", self.tabArray[i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        if (i == 0) {
            [self tabButton:button];
        }
        
        [self.topBtns addObject:button];
        [self.topScrollView addSubview:view];
    }
}

#pragma mark --点击顶部的按钮所触发的方法
- (void)tabButton:(id)sender
{
    UIButton *button = sender;
    if (self.currentBtn != button) {
        button.selected = YES;
        self.currentBtn.selected = NO;
        self.currentBtn = button;
    }
    
    [self.scrollView setContentOffset:CGPointMake(button.tag * LXMainViewControllerViewWidth, 0) animated:YES];
}

#pragma mark -- 实例化底部ScrollView
- (void)initScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LXMainViewControllerTopMainViewHeight + 10, LXMainViewControllerViewWidth, LXMainViewControllerViewHeight - LXMainViewControllerTopMainViewHeight)];
    self.scrollView.contentSize = CGSizeMake(LXMainViewControllerViewWidth * self.tabArray.count, LXMainViewControllerViewHeight - LXMainViewControllerTopMainViewHeight);
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    [self.view addSubview:self.scrollView];
    
}

#pragma mark --初始化下方的TableViews
- (void)initDownTables
{
    /*** 推荐模块控制器 */
    LXRecommendTableViewController *recommendTableViewController = [[LXRecommendTableViewController alloc]init];
    [self addTableView:recommendTableViewController tag:0];

    /*** 动画模块控制器 */
    LXOperaTableViewController *operaTableViewController = [[LXOperaTableViewController alloc]init];
    [self addTableView:operaTableViewController tag:1];
    
    /*** 音乐模块控制器 */
    LXEntertainmentTableViewController *entertainmentTableViewController = [[LXEntertainmentTableViewController alloc]init];
    [self addTableView:entertainmentTableViewController tag:2];

    /*** 分区模块控制器 */
    LXTypeTableViewController  *typeTableViewController = [[LXTypeTableViewController alloc]init];
    typeTableViewController.view.frame = CGRectMake(3 * LXMainViewControllerViewWidth, 0, LXMainViewControllerViewWidth, LXMainViewControllerViewHeight - TOPHEIGHT);
    [self.scrollTableViewsController addObject:typeTableViewController];
    [self.scrollView addSubview:typeTableViewController.view];
    [self addChildViewController:typeTableViewController];
    
    /*** 我的模块控制器 */
    LXMineTableViewController *mineTableViewController = [[LXMineTableViewController alloc]init];
    mineTableViewController.myDelegate = self;
    mineTableViewController.view.frame = CGRectMake(4 * LXMainViewControllerViewWidth, 0, LXMainViewControllerViewWidth, LXMainViewControllerViewHeight - TOPHEIGHT);
    [self.scrollTableViewsController addObject:mineTableViewController];
    [self.scrollView addSubview:mineTableViewController.view];
    [self addChildViewController:mineTableViewController];

}

#pragma mark 添加tableView视图
- (void)addTableView:(UITableViewController *)tableViewController tag:(int)tag
{
    tableViewController.tableView.frame = CGRectMake(tag * LXMainViewControllerViewWidth, 0, LXMainViewControllerViewWidth, LXMainViewControllerViewHeight - LXMainViewControllerTopMainViewHeight - 20);
    [self.scrollTableViewsController addObject:tableViewController];
    [self.scrollView addSubview:tableViewController.tableView];
    
    [self addChildViewController:tableViewController];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([self.scrollView isEqual:scrollView])
    {
        int num = self.scrollView.contentOffset.x / self.view.frame.size.width;
        [self tabButton:self.topBtns[num]];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if ([self.scrollView isEqual:scrollView])
    {
        CGRect frame = self.slideView.frame;
        if (self.tabArray.count <= 6)
        {
            frame.origin.x = scrollView.contentOffset.x/self.tabArray.count;
        } else {
            frame.origin.x = scrollView.contentOffset.x/6;
        }
        self.slideView.frame = frame;
    }
    
}

#pragma mark - LXNavBarView的代理
- (void)mainNavBarView:(LXMainNavBarView *)navBar
{
    LXSearchTableViewController *searchVc = [[LXSearchTableViewController alloc]init];
    [self.navigationController pushViewController:searchVc animated:YES];
}

#pragma mark - LXMineTableViewController
- (void)pushLoginViewController:(LXMineTableViewController *)mineTableViewController pushVC:(UIViewController *)pushVC
{
    [self.navigationController pushViewController:pushVC animated:YES];
}

@end
